#!/usr/bin/env ruby
# - runtime
# - nr_threads
# - blocksize
# - iterations

## pigz, which stands for parallel implementation of gzip, is a fully
## functional replacement for gzip that exploits multiple processors
## and multiple cores to the hilt when compressing data.

require 'base64'

runtime = (ENV['runtime'] || 300).to_i
nr_threads = ENV['nr_threads'] || ENV['nr_cpu']
blocksize = (ENV['blocksize'] || 128).to_i
total_iterations = (ENV['iterations'] || 0).to_i

case File.basename($PROGRAM_NAME)
when /pigz/
  pigz_threads = "-p #{nr_threads}" if nr_threads
  pigz_threads ||= ''
  run_cmd = "pigz -c -b #{blocksize} #{pigz_threads}"
when /pbzip2/
  pbzip2_blocksize = ENV['blocksize'] || 900
  pbzip2_blocksize = "-b#{pbzip2_blocksize.to_i / 100}"
  pbzip2_threads = "-p#{nr_threads}" if nr_threads
  pbzip2_threads ||= ''
  run_cmd = "pbzip2 -c #{pbzip2_blocksize} #{pbzip2_threads}"
when /pixz/
  pixz_threads = "-p #{nr_threads}" if nr_threads
  pixz_threads ||= ''
  run_cmd = "pixz #{pixz_threads}"
when /plzip/
  plzip_threads = "-n #{nr_threads}" if nr_threads
  plzip_threads ||= ''
  run_cmd = "plzip -c #{plzip_threads}"
when /pxz/
  pxz_threads = "-T #{nr_threads}" if nr_threads
  pxz_threads ||= ''
  run_cmd = "pxz -c #{pxz_threads}"
end

exit unless run_cmd
puts "# run_cmd: #{run_cmd}"

F_LINUX_SPECIFIC_BASE = 1024
F_SETPIPE_SZ = F_LINUX_SPECIFIC_BASE + 7

REPEAT = 5
MB = (1 << 20)

data = Base64.encode64 Random.new(1234).bytes(MB)
iterations = 0

start_time = Time.now
1.upto(REPEAT) do
  now = 0
  time = Time.now
  io = IO.popen("#{run_cmd} > /dev/null", 'w')
  io.fcntl(F_SETPIPE_SZ, MB)

  i = 0
  loop do
    io.puts data
    i += 1
    now = Time.now
    if total_iterations.zero?
      break if now - time > runtime / REPEAT
    elsif i == total_iterations / REPEAT
      break
    end
  end

  io.close
  puts "# #{i} iterations in #{now - time} seconds"
  iterations += i
end
seconds = Time.now - start_time

puts "throughput: #{MB * iterations / seconds}"
